home *** CD-ROM | disk | FTP | other *** search
/ Chip 2006 June / CHIP 2006-06.2.iso / program / freeware / Democracy-0.8.2.exe / xulrunner / python / autodler.py < prev    next >
Encoding:
Python Source  |  2006-04-10  |  4.5 KB  |  146 lines

  1. from random import randint
  2. from threading import Lock
  3.  
  4. import feed
  5. import item
  6. import config
  7. import database
  8. import scheduler
  9.  
  10. ##
  11. # Runs in the background and automatically triggers downloads
  12. class AutoDownloader:
  13.     ##
  14.     # Returns true iff x is an autodownloader
  15.     def isAutoDownloader(self,x):
  16.         ret = False
  17.         if x.getAutoDownloaded() and x.getState() == 'downloading':
  18.             ret = True
  19.         return ret
  20.  
  21.     ##
  22.     # Returns true iff x is a manual downloader
  23.     def isManualDownloader(self,x):
  24.         ret = False
  25.         if not x.getAutoDownloaded() and x.getState() == 'downloading':
  26.             ret = True
  27.         return ret
  28.  
  29.     ##
  30.     # returns the number of automatic downloads currently happening
  31.     def autoDownloads(self):
  32.         count = 0
  33.         for dl in self.autoDownloaders:
  34.             count += 1
  35.         return count
  36.  
  37.     ##
  38.     # returns the number of manual downloads currently happening
  39.     def manualDownloads(self):
  40.         count = 0
  41.         for dl in self.manualDownloaders:
  42.             count += 1
  43.         return count
  44.  
  45.     ##
  46.     # Returns true iff x is a feed and is automatically downloadable
  47.     def eligibileFeedFilter(self,x):
  48.         return x.isAutoDownloadable()
  49.  
  50.     ##
  51.     # Returns true iff x is a feed with a manual download item
  52.     def manualFeedFilter(self,x):
  53.         ret = False
  54.         x.beginRead()
  55.         try:
  56.             for item in x.items:
  57.                 if item.getState() == 'manualpending':
  58.                     ret = True
  59.                     break
  60.         finally:
  61.             x.endRead()
  62.         return ret
  63.                 
  64.     ##
  65.     # This triggers items to be expired
  66.     def expireItems(self):
  67.         for feed in self.allFeeds:
  68.             feed.expireItems()
  69.  
  70.     ##
  71.     # This is the function that actually triggers the downloads It
  72.     # loops through all of the available feeds round-robin style and
  73.     # gets the next thing it can
  74.     # 
  75.     def spawnDownloads(self):
  76.         attempts = 0
  77.         numFeeds = self.autoFeeds.len()
  78.         numDownloads = self.autoDownloads()
  79.         target = config.get(config.DOWNLOADS_TARGET)
  80.         while numDownloads < target and numFeeds > attempts:
  81.             attempts += 1
  82.             thisFeed = self.autoFeeds.getNext()
  83.             if thisFeed == None:
  84.                 self.autoFeeds.resetCursor()
  85.                 thisFeed = self.autoFeeds.getNext()
  86.             if thisFeed != None:
  87.                 thisFeed.downloadNextAuto()
  88.                 numDownloads += 1
  89.  
  90.         attempts = 0
  91.         numFeeds = self.manualFeeds.len()
  92.         numDownloads = self.manualDownloads()
  93.         target = config.get(config.MAX_MANUAL_DOWNLOADS)
  94.         #print "I have %d manual downloads in %d feeds. I'm looking for %d" % (
  95.         #    numDownloads,numFeeds,target)
  96.         while (numDownloads < target and numFeeds > attempts):
  97.             #print "."
  98.             attempts += 1
  99.             thisFeed = self.manualFeeds.getNext()
  100.             if thisFeed == None:
  101.                 self.manualFeeds.resetCursor()
  102.                 thisFeed = self.manualFeeds.getNext()
  103.             if thisFeed != None:
  104.                 thisFeed.downloadNextManual()
  105.                 numDownloads += 1
  106.  
  107.     def run(self):
  108.         self.lock.acquire()
  109.         try:
  110.             if self.running:
  111.                 return
  112.             else:
  113.                 self.running = True
  114.         finally:
  115.             self.lock.release()
  116.         self.expireItems()
  117.         self.spawnDownloads()
  118.         self.lock.acquire()
  119.         try:
  120.             self.running = False
  121.         finally:
  122.             self.lock.release()
  123.  
  124.     def __init__(self):
  125.         self.lock = Lock()
  126.         self.running = False
  127.         self.allFeeds = database.defaultDatabase.filter(lambda x:isinstance(x,feed.Feed))
  128.         self.allItems = database.defaultDatabase.filter(lambda x:isinstance(x,item.Item))
  129.         self.autoFeeds =self.allFeeds.filter(self.eligibileFeedFilter)
  130.         if self.autoFeeds.len() > 1:
  131.             skip = randint(0,self.autoFeeds.len()-1)
  132.             for x in range(0,skip):
  133.                 self.autoFeeds.getNext()
  134.  
  135.         self.manualFeeds = self.allFeeds.filter(self.manualFeedFilter)
  136.         if self.manualFeeds.len() > 1:
  137.             skip = randint(0,self.manualFeeds.len()-1)
  138.             for x in range(0,skip):
  139.                 self.manualFeeds.getNext()
  140.  
  141.         self.autoDownloaders = self.allItems.filter(self.isAutoDownloader)
  142.         self.manualDownloaders = self.allItems.filter(self.isManualDownloader)
  143.  
  144.         self.run()
  145.         self.event = scheduler.ScheduleEvent(10,self.run)
  146.